package com.unbiz.common.bean;

import com.unbiz.common.mutable.PropertyValue;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.*;
import javafx.beans.value.*;

class Bill {

    //定义一个变量来保存属性

    private DoubleProperty amountDue = new SimpleDoubleProperty();
    
    public final PropertyValue<Double> x = new PropertyValue<Double>(1.0);

    //定义一个getter方法来获取属性的值

    public final double getAmountDue(){return amountDue.get();}

    //定义一个setter来设定属性的值

    public final void setAmountDue(double value){amountDue.set(value);}

     //定义一个getter来访问属性

    public DoubleProperty amountDueProperty() {return amountDue;}

}

public class BindingSample {
	public static void main(String[] args) {

        Bill electricBill = new Bill();

        electricBill.amountDueProperty().addListener(new ChangeListener<Number>(){

         @Override 
         public void changed(ObservableValue o,Number oldVal, Number newVal){

            System.out.println("Electric bill has changed!");

         }

      });

      electricBill.setAmountDue(100.00);
      
      
      electricBill.x.addListener(new ChangeListener<Double>(){

         @Override 
         public void changed(ObservableValue o,Double oldVal, Double newVal){

            System.out.println("bill.x has changed!");

         }

      });
      
      electricBill.x.set(2.0);
      
      
      IntegerProperty num1 = new SimpleIntegerProperty(1);

      IntegerProperty num2 = new SimpleIntegerProperty(2);

      IntegerProperty num3 = new SimpleIntegerProperty(3);

      IntegerProperty num4 = new SimpleIntegerProperty(4);

      NumberBinding total =  Bindings.add(num1.multiply(num2),num3.multiply(num4));
      
      total.addListener(new InvalidationListener() {

          @Override 
          public void invalidated(Observable o) {

                System.out.println("The binding is now invalid.");

            }

         });

      System.out.println(total.getValue());

      num1.setValue(2);

      System.err.println(total.getValue());
	}
	
}
